package com.suke.czx.modules.task.controller;

import com.suke.czx.common.annotation.SysLog;
import com.suke.czx.common.base.AbstractController;
import com.suke.czx.common.utils.*;
import com.suke.czx.common.validate.TaskSaveGroup;
import com.suke.czx.common.validate.TaskUpdateGroup;
import com.suke.czx.modules.task.entity.Task;
import com.suke.czx.modules.task.entity.TaskSearchBo;
import com.suke.czx.modules.task.service.TaskService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 应用模块名称: 任务
 * 代码描述: 任务控制器
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/11 17:31
 */
@Validated
@RestController
@RequestMapping("/api/task")
@AllArgsConstructor
@Api(value = "TaskController", tags = "任务管理")
public class TaskController extends AbstractController {

    private final TaskService taskService;

    /**
     * 分页查询任务列表
     * @param taskSearchBo 查询BO
     * @return VO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:task:list')")
    public R list(TaskSearchBo taskSearchBo) {
        if (taskSearchBo.getPage() == null || taskSearchBo.getLimit() == null) {
            return R.error("缺少分页信息");
        }
        // 分页查询任务列表
        return taskService.pageTaskList(taskSearchBo);
    }

    /**
     * 保存任务
     * @param task 任务
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("保存任务")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:firstTask:save') or hasRole('but:secondTask:save') or hasRole('but:thirdTask:save')")
    @Transactional(rollbackFor = Exception.class)
    public R save(@Validated(TaskSaveGroup.class) Task task, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        return taskService.saveTask(task, getUserId());
    }

    /**
     * 更新任务
     * @param task 任务
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("更新任务")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:firstTask:update') or hasRole('but:secondTask:update') or hasRole('but:thirdTask:update')")
    public R update(@Validated(TaskUpdateGroup.class) Task task, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        task.setUpdater(getUserId());
        task.setUpdateTime(new Date());
        taskService.updateById(task);
        return R.ok();
    }

    /**
     * 删除任务
     * @param taskId 任务编码
     * @return VO
     */
    @SysLog("删除任务")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:firstTask:delete') or hasRole('but:secondTask:delete') or hasRole('but:thirdTask:delete')")
    public R delete(@NotNull(message = "任务编码不能为空") Long taskId) {
        return taskService.updateDelTask(taskId, getUserId());
    }

    /**
     * 二级任务状态
     * @param taskId 任务编码
     * @param taskLevel 任务级别
     * @param progress 二级任务状态
     * @return VO
     */
    @SysLog("二级任务状态")
    @RequestMapping(value = "/progress", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:task:progress')")
    public R progress(@NotNull(message = "任务编码不能为空") Long taskId, @NotNull(message = "任务级别不能为空") Integer taskLevel,
                      @NotNull(message = "二级任务状态不能为空") Integer progress) {
        if (taskLevel == null || taskLevel != CodeConstant.TASK_LEVEL_SECOND) {
            return R.error("任务级别错误");
        }
        return taskService.updateTaskProgress(taskId, taskLevel, progress, getUserId());
    }

    /**
     * 三级任务状态
     * @param taskId 任务编码
     * @param taskLevel 任务级别
     * @param status 三级任务状态
     * @return VO
     */
    @SysLog("三级任务状态")
    @RequestMapping(value = "/status", method = RequestMethod.POST)
     @PreAuthorize("hasRole('but:task:status')")
    public R status(@NotNull(message = "任务编码不能为空") Long taskId, @NotNull(message = "任务级别不能为空") Integer taskLevel,
                    @NotNull(message = "三级任务状态不能为空") Integer status) {
        if (taskLevel == null || taskLevel != CodeConstant.TASK_LEVEL_THIRD) {
            return R.error("任务级别错误");
        }
        return taskService.updateTaskStatus(taskId, taskLevel, status, getUserId());
    }

}